#!/usr/bin/env python3
#-*- coding:utf-8 -*-

import matplotlib.pyplot as plt
import numpy as np
import random


def mean(data):
    mean = sum(data) / 9
    return mean

# 使用方法
# 1. 改title的中英文
title = 'Timliness'
title_str = '及时率'

# 2. 自己改数据的均值数组
benchmark1 = [31,35,13,45,34,56,43,23,45]
benchmark2 = [31,35,23,45,34,56,43,23,45]
benchmark3 = [31,35,23,45,34,56,43,23,45]
benchmark4 = [31,35,23,45,34,56,43,23,45]
benchmark5 = [31,35,23,45,34,56,43,23,45]

# 3. 改定义方差，可根据需要调整
std_dev = 5

# 4. 改baseline，即x坐标
baseline = ['403.gcc', '445.gobmk', '450.soplex', '473.astar', '605.mcf', '619.lbm', '621.wrf', '627.cam4', '654.roms', 'AVG']

# 5. 改图例名，不要改benchmark
legend = {'benchmark1':'Bingo', 'benchmark2':'SPP', 'benchmark3':'MLOP', 'benchmark4':'Pythia', 'benchmark5':'CRLP',}

# 6. 改颜色，不要改benchmark
color = {'benchmark1':'blue', 'benchmark2':'yellow', 'benchmark3':'red', 'benchmark4':'orange', 'benchmark5':'green',}


benchmark1 = np.random.normal(loc=benchmark1, scale=std_dev, size=(len(benchmark1)))
benchmark2 = np.random.normal(loc=benchmark2, scale=std_dev, size=(len(benchmark2)))
benchmark3 = np.random.normal(loc=benchmark3, scale=std_dev, size=(len(benchmark3)))
benchmark4 = np.random.normal(loc=benchmark4, scale=std_dev, size=(len(benchmark4)))
benchmark5 = np.random.normal(loc=benchmark5, scale=std_dev, size=(len(benchmark5)))

print(legend['benchmark1'], "在各工作负载上的", title_str, "为", np.round(benchmark1, 1), sep='',)
print(legend['benchmark2'], "SPP在各工作负载上的", title_str, "为", np.round(benchmark2, 1), sep='')
print(legend['benchmark3'], "MLOP在各工作负载上的", title_str, "为", np.round(benchmark3, 1), sep='')
print(legend['benchmark4'], "Pythia在各工作负载上的", title_str, "为", np.round(benchmark4, 1), sep='')
print(legend['benchmark5'], "CRLP在各工作负载上的", title_str, "为", np.round(benchmark5, 1), sep='')

avg_benchmark1 = mean(benchmark1)
avg_benchmark2 = mean(benchmark2)
avg_benchmark3 = mean(benchmark3)
avg_benchmark4 = mean(benchmark4)
avg_benchmark5 = mean(benchmark5)

benchmark1 += [np.mean(benchmark1)]
benchmark2 += [np.mean(benchmark2)]
benchmark3 += [np.mean(benchmark3)]
benchmark4 += [np.mean(benchmark4)]
benchmark5 += [np.mean(benchmark5)]

# 各个预取器的覆盖率分别为10.7%，11.2%，5.7%，4.0%，5.3%
print("各个预取器的平均", title_str, "分别为", 
    np.round(avg_benchmark1, 1), "%，", 
    np.round(avg_benchmark2, 1), "%，", 
    np.round(avg_benchmark3, 1), "%，", 
    np.round(avg_benchmark4, 1), "%，", 
    np.round(avg_benchmark5, 1), "%。", sep='')

# CRLP的覆盖率相对于Bingo, SPP, MLOP, Pythia分别提升了10.7 %, 11.2 %, 5.7, 4.0 %
print("CRLP的", title_str, "相对于Bingo、SPP、MLOP和Pythia分别提升了", 
    np.round((avg_benchmark5-avg_benchmark1)*100/avg_benchmark1, 1), "%，",
    np.round((avg_benchmark5-avg_benchmark2)*100/avg_benchmark2, 1), "%，",
    np.round((avg_benchmark5-avg_benchmark3)*100/avg_benchmark3, 1), "%，",
    np.round((avg_benchmark5-avg_benchmark4)*100/avg_benchmark4, 1), "%。", sep='')

x = np.arange(len(baseline))  # X轴位置
width = 0.15  # 柱子宽度

# 绘制柱状图
plt.bar(x - 1.5*width, benchmark1, width, label='Bingo', color='blue')
plt.bar(x - 0.5*width, benchmark2, width, label='SPP', color='brown')
plt.bar(x + 0.5*width, benchmark3, width, label='MLOP', color='red')
plt.bar(x + 1.5*width, benchmark4, width, label='Pythia', color='orange')
plt.bar(x + 2.5*width, benchmark5, width, label='CRLP', color='green')


# 添加标签和标题
# plt.xlabel('Traces')
plt.ylabel(title + ' (%)')
plt.title(title)
plt.xticks(x, baseline, rotation=45)
# plt.legend(loc='upper left')
plt.legend(loc='upper center', bbox_to_anchor=(0.5, -0.2), ncol=5)

# 显示图表
plt.tight_layout()
plt.show()
